﻿using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using TSOL.FastSPWeb;
using TSOL.Helpers;

namespace TSOL.FastSPWebTester
{
    [ToolboxItemAttribute(false)]
    public class FastSPWebTesterWP : WebPart
    {
        protected override void Render(HtmlTextWriter writer)
        {
            string FastWebOutputText = "";

            FastSPWebCall.OpenWeb("siteurl", "weburl",
                (_site, _web, hashCode) =>
                {
                    SPList _list = null;
                    _web.FastLists(FastSPWebCallExtensions.CachePolicy.FastSPDefault, (Lists) =>
                    {
                        _list = Lists["ListName"];
                    });

                    int count = 0;
                    _list.FastItems(FastSPWebCallExtensions.CachePolicy.FastSPDefault, (Items) =>
                    {
                        count = Items.Count;
                    });
                    FastWebOutputText += "Writing fra threadpool, " + hashCode + ", items " + count.ToString();
                });
            FastSPWebCall.OpenWeb(SPContext.Current.Web.CurrentUser, "siteurl", "",
                (_site, _web, hashCode) =>
                {
                    SPList _list = null;
                    _web.FastLists(FastSPWebCallExtensions.CachePolicy.FastSPDefault, (Lists) =>
                    {
                        _list = Lists["listname"];
                    });
                    int count = 0;
                    _list.FastItems(FastSPWebCallExtensions.CachePolicy.FastSPDefault, (Items) =>
                    {
                        count = Items.Count;
                    });
                    FastWebOutputText += "usertoken, writing fra threadpool, " + hashCode + ", items " + count.ToString();
                });

            writer.Write(
                new Panel() { BorderStyle = System.Web.UI.WebControls.BorderStyle.None, BorderWidth = 0, Width = 320 }.AddChilds(
                    (GroupControls) =>
                    {
                        GroupControls.Add(new TextBox() { ID = "FastWebTesterTextBox", Width = 300, TextMode = TextBoxMode.SingleLine, Text = FastWebOutputText });
                    }
                ).RenderToString()
            );
            base.Render(writer);
        }

    }
}
